package crazyFace.system.app;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

public class SavingSystem{
	
	GameView gameView = null;
	
	public SavingSystem(GameView gameview){
		gameView = gameview;
	}
	
	public void save(){
		ArrayList<String> tempArray = new ArrayList<String>();
		String[] data = null;
		String player = "player: <" + "ammo-" + GameView.playerHolder.get(0).ammo +
				"> <life-" + GameView.playerHolder.get(0).life + "> <maxlife-" + GameView.playerHolder.get(0).maxHealth +
				"> <x-" + GameView.playerHolder.get(0).x + "> <y-" + GameView.playerHolder.get(0).y +
				"> <rifle-" + GameView.hasRifle + "> <tnt-" + GameView.hasTNT + ">;";
		tempArray.add(player);
		
		String score = "score: <" + GameView.score + ">;";
		tempArray.add(score);
		
		String monstersspawned = "monstersspawned: <" + GameView.monstersSpawned + ">;";
		tempArray.add(monstersspawned);
		
		String gamedif = "gamedif: <" + GameView.gameDif + ">;";
		tempArray.add(gamedif);
		
		String sys = "system: <" + "hardcore-" + GameView.hardcore + "> <minutes-" + GameView.minutes + "> <seconds-" + GameView.seconds + "> <xscroll-" + GameView.xScroll + "> <yscroll-" + GameView.yScroll + ">;";
		tempArray.add(sys);		
		
		for(int i = 0; i < GameView.enameys.size(); i++){
			String enamy = "monster: <" + "objectid-" + GameView.enameys.get(i).ObjectID + "> <x-" +
					(int) GameView.enameys.get(i).x + "> <y-" + (int) GameView.enameys.get(i).y + "> <life-" +
					(int) GameView.enameys.get(i).life + ">;";
			tempArray.add(enamy);
		}
		for(int i = 0; i < GameView.bullets.size(); i++){
			String bullet = "bullet: <" + "x-" + GameView.bullets.get(i).x + "> <y-" + GameView.bullets.get(i).y +
					"> <direction-" + GameView.bullets.get(i).aniRow + ">;";
			tempArray.add(bullet);
		}
		for(int i = 0; i < GameView.coins.size(); i++){
			String coin = "coin: <" + "x-" + GameView.coins.get(i).x + "> <y-" + GameView.coins.get(i).y + ">;";
			tempArray.add(coin);
		}
		for(int i = 0; i < GameView.items.size(); i++){
			String item = "item: <" + "objectid-" + GameView.items.get(i).ObjectID + "> <x-" + 
					GameView.items.get(i).x + "> <y-" + GameView.items.get(i).y + ">;";
			tempArray.add(item);
		}
				
		data = new String[tempArray.size()];
		
		for(int i = 0; i < tempArray.size(); i++){
			data[i] = tempArray.get(i);
		}
		
		putSave("/save.sav", data);
	}
	
	public void load(){
		GameView.loading = true;
		GameView.loadtime = System.currentTimeMillis() + 10000;
		String[] data = getSave("/save.sav");
		
		for(String save : data){
			if(save.contains("player:")){
				
				int count = this.count(save, '<');
				String tempPart1, tempPart2, tempPart3;
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;

					if(tempPart2.contains("ammo")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.playerHolder.get(0).ammo = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("<life")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.playerHolder.get(0).life = java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("maxlife")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.playerHolder.get(0).maxHealth = java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("x")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.playerHolder.get(0).x = java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("y")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.playerHolder.get(0).y = java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("rifle")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.hasRifle = java.lang.Boolean.parseBoolean(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("tnt")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.hasTNT = java.lang.Boolean.parseBoolean(tempPart2.substring(t1, t2));
					}
				}
			}else if(save.contains("score:")){
				
				String tempPart1, tempPart2, tempPart3;
				
				int temp1 = save.indexOf("<");
				int temp2 = save.indexOf(">");
				temp2++;
				tempPart1 = save.substring(0, temp1);
				tempPart2 = save.substring(temp1, temp2);
				tempPart3 = save.substring(temp2, save.length());
				save = tempPart1 + tempPart3;
				
				int t1 = tempPart2.indexOf("<");
				t1++;
				int t2 = tempPart2.indexOf(">");
				GameView.score = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
				
			}else if(save.contains("monstersspawned:")){
				String tempPart1, tempPart2, tempPart3;
				
				int temp1 = save.indexOf("<");
				int temp2 = save.indexOf(">");
				temp2++;
				tempPart1 = save.substring(0, temp1);
				tempPart2 = save.substring(temp1, temp2);
				tempPart3 = save.substring(temp2, save.length());
				save = tempPart1 + tempPart3;
				
				int t1 = tempPart2.indexOf("<");
				t1++;
				int t2 = tempPart2.indexOf(">");
				GameView.monstersSpawned = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
				
			}else if(save.contains("gamedif:")){
				String tempPart1, tempPart2, tempPart3;
				
				int temp1 = save.indexOf("<");
				int temp2 = save.indexOf(">");
				temp2++;
				tempPart1 = save.substring(0, temp1);
				tempPart2 = save.substring(temp1, temp2);
				tempPart3 = save.substring(temp2, save.length());
				save = tempPart1 + tempPart3;
				
				int t1 = tempPart2.indexOf("<");
				t1++;
				int t2 = tempPart2.indexOf(">");
				GameView.gameDif = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
				
			}else if(save.contains("system:")){
				
				int count = this.count(save, '<');
				String tempPart1, tempPart2, tempPart3;
				
				int minutes = 0;
				int seconds = 0;
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;
					if(tempPart2.contains("hardcore")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.hardcore = java.lang.Boolean.parseBoolean(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("minutes")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						minutes = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("seconds")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						seconds = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("xscroll")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.xScroll = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("yscroll")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						GameView.yScroll = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}
					
					long timeLeft = ((minutes * 60) + seconds) * 1000;
					
					if(GameView.hardcore){
			    		GameView.beginTime = (System.currentTimeMillis() - (2700000 - timeLeft));
			    	}else if(GameView.gameDif == 1){
			    		GameView.beginTime = (System.currentTimeMillis() - (3600000 - timeLeft));
			    	}else if(GameView.gameDif == 2){
			    		GameView.beginTime = (System.currentTimeMillis() - (3300000 - timeLeft));
			    	}else if(GameView.gameDif == 3){
			    		GameView.beginTime = (System.currentTimeMillis() - (3000000 - timeLeft));
			    	}
				}
			}else if(save.contains("monster:")){
				
				int count = this.count(save, '<');
				String tempPart1, tempPart2, tempPart3;
				int tempx = 0;
				int tempy = 0;
				int objectid = 0;
				int life = 0;
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;
					if(tempPart2.contains("objectid")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						objectid = java.lang.Integer.parseInt(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("x")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempx = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("life")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						life = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("y")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempy = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}
					
					if(objectid == ObjectIDs.skeli){
						Skeli skeli = gameView.createSkeli(tempx, tempy, life);
						GameView.enameys.add(skeli);
					}else if(objectid == ObjectIDs.spider){
						Spider spider = gameView.createSpider(tempx, tempy, life);
						GameView.enameys.add(spider);
					}else if(objectid == ObjectIDs.bomeber){
						Bomber bomber = gameView.createBomber(tempx, tempy, life);
						GameView.enameys.add(bomber);
						System.out.println(tempx);
					}else if(objectid == ObjectIDs.multifyer){
						Multifyer multifyer = gameView.createMultifyer(tempx, tempy, life);
						GameView.enameys.add(multifyer);
					}else if(objectid == ObjectIDs.raper){
						Raper raper = gameView.createRaper(tempx, tempy, life);
						GameView.enameys.add(raper);
					}else if(objectid == ObjectIDs.fireball){
						TrackerBall fireball = gameView.createBall(R.drawable.fire_ball, tempx, tempy, ObjectIDs.fireball, life);
						GameView.enameys.add(fireball);
					}else if(objectid == ObjectIDs.electricball){
						TrackerBall electricball = gameView.createBall(R.drawable.lightning_ball, tempx, tempy, ObjectIDs.electricball, life);
						GameView.enameys.add(electricball);
					}else if(objectid == ObjectIDs.shadowball){
						TrackerBall shadowball = gameView.createBall(R.drawable.shadow_ball, tempx, tempy, ObjectIDs.shadowball, life);
						GameView.enameys.add(shadowball);
					}else if(objectid == ObjectIDs.boss1){
						Boss1 boss1 = gameView.createBoss1(tempx, tempy, life);
						GameView.enameys.add(boss1);
					}else if(objectid == ObjectIDs.boss2){
						Boss2 boss2 = gameView.createBoss2(tempx, tempy, life);
						GameView.enameys.add(boss2);
					}else if(objectid == ObjectIDs.boss3){
						Boss3 boss3 = gameView.createBoss3(tempx, tempy, life);
						GameView.enameys.add(boss3);
					}else if(objectid == ObjectIDs.boss4){
						Boss4 boss4 = gameView.createBoss4(tempx, tempy, life);
						GameView.enameys.add(boss4);
					}else if(objectid == ObjectIDs.boss5){
						Boss5 boss5 = gameView.createBoss5(tempx, tempy, life);
						GameView.enameys.add(boss5);
					}else if(objectid == ObjectIDs.finalboss){
						FinalBoss finalboss = gameView.createFinalBoss(tempx, tempy, life);
						GameView.enameys.add(finalboss);
					}
				}
				
				gameView.createCoin(tempx, tempy);
			}else if(save.contains("coin:")){
				
				String tempPart1, tempPart2, tempPart3;
				int tempx = 0;
				int tempy = 0;
				int count = this.count(save, '<');
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;
					if(tempPart2.contains("x")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempx = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("y")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempy = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}
				}
				gameView.createCoin(tempx, tempy);
			}else if(save.contains("bullet:")){
				
				int count = this.count(save, '<');
				String tempPart1, tempPart2, tempPart3;
				int tempx = 0;
				int tempy = 0;
				int tempd = 0;
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;
					if(tempPart2.contains("x")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempx = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("y")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempy = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("direction")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempd = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}
				}
				
				gameView.createBullet(tempd, tempx, tempy);
			}else if(save.contains("item:")){
				
				int count = this.count(save, '<');
				String tempPart1, tempPart2, tempPart3;
				int tempx = 0;
				int tempy = 0;
				int tempid = 0;
				
				for(int i = 0; i < count; i++){
					int temp1 = save.indexOf("<");
					int temp2 = save.indexOf(">");
					temp2++;
					tempPart1 = save.substring(0, temp1);
					tempPart2 = save.substring(temp1, temp2);
					tempPart3 = save.substring(temp2, save.length());
					save = tempPart1 + tempPart3;
					if(tempPart2.contains("x")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempx = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("y")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempy = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}else if(tempPart2.contains("objectid")){
						int t1 = tempPart2.indexOf("-");
						t1++;
						int t2 = tempPart2.indexOf(">");
						tempid = (int) java.lang.Float.parseFloat(tempPart2.substring(t1, t2));
					}
					
				}
				
				if(tempid == ObjectIDs.heath){
					gameView.createHeal(tempx, tempy);
				}else if(tempid == ObjectIDs.rifle){
					gameView.createRifle(R.drawable.rifle, tempx, tempy);
				}else if(tempid == ObjectIDs.tnt){
					gameView.createTNT(R.drawable.tnt, tempx, tempy);
				}
			}
		}
	}
	
	public void putSave(String file, String[] words){
		FileOutputStream fOut = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
        	
        	fOut = new FileOutputStream(new File(GameView.saveFolder + file));
            osw = new OutputStreamWriter(fOut);
            bw = new BufferedWriter(osw);
            for(int i = 0; i < words.length; i++){
            	bw.write(words[i]);
            	if(i < words.length){
            		bw.newLine();
            	}
            }
            bw.flush();
            bw.close();
        }

        catch (Exception e) {

            e.printStackTrace();
        }
	}
	
	public String[] getSave(String file){
        InputStreamReader isr = null;
        FileInputStream fIn = null;
        BufferedReader br = null;
        String lineBuffer = null;
        List<String> buffer = new ArrayList<String>();
        try {
        	fIn = new FileInputStream(new File(GameView.saveFolder + file));
        	isr = new InputStreamReader(fIn);
        	br = new BufferedReader(isr);
        	while ((lineBuffer = br.readLine()) != null) {
                buffer.add(lineBuffer);
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
        String[] data = buffer.toArray(new String[buffer.size()]);
        
        return data;
	}
	
	public int count(String string, char charector){
		int count = 0;
		for(int i = 0; i < string.length(); i++){
			if(string.charAt(i) == charector){
				count++;
			}
		}
		
		return count;
	}
}
